#pragma once
#include<string>
#include <netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>

class InetAddr
{
public:
    InetAddr(struct sockaddr_in& addr)
    :_addr(addr)
    {
        char buffer[64];
        inet_ntop(AF_INET,&_addr.sin_addr.s_addr,buffer,sizeof(buffer));
        _ip  = buffer;
        _port = ntohs(_addr.sin_port);
    }

    InetAddr(const std::string& ip,uint16_t port)
    :_ip(ip)
    ,_port(port)
    {
        bzero(&_addr,sizeof(_addr));
        _addr.sin_family =  AF_INET;
        _addr.sin_port = htons(_port);
        inet_pton(AF_INET,_ip.c_str(),&_addr.sin_addr);
    }

    ~InetAddr()
    {

    }

    const sockaddr_in& GetAddr() const
    {
        return _addr;
    }

    std::string GetIp() const
    {
        return _ip;
    }

    uint16_t GetPort()  const
    {
        return _port;
    }

    bool operator==(const InetAddr& temp)
    {
        if(_ip == temp._ip && _port == temp._port)
        {
            return true;
        }
        return false;
    }

private:
    struct sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;
};